package datacollection;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SyncFailedException;

import javax.swing.JButton;
import javax.swing.JFrame;

import bot.Util;

public class UsCensusCollectionBot {
	
	Robot robo;
	
	public UsCensusCollectionBot(){
		try {
			robo = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
	
	public void saveCityInfo(String filename){
		WebParser web = new WebParser();
		int k = 903;
		while (true){
			String newName = filename.substring(0, filename.length() - 4) + k + filename.substring(filename.length() - 4, filename.length());
			Util.setPasteBuffer(newName);
			//move to the drop down menu
			//was 310, 490 prior to advanced search thing
			robo.mouseMove(310, 555);
			long start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 400);
			robo.mousePress(InputEvent.BUTTON1_MASK);
			start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 400);
			robo.mouseRelease(InputEvent.BUTTON1_MASK);
			start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 400);
			//move the the next city down
			robo.keyPress(KeyEvent.VK_DOWN);
			robo.keyRelease(KeyEvent.VK_DOWN);
			start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 400);
			//click that city
			robo.keyPress(KeyEvent.VK_ENTER);
			robo.keyRelease(KeyEvent.VK_ENTER);
			//wait for loading box to pop up 564, 400
			while (robo.getPixelColor(400, 490).equals(new Color(255, 255, 255))){}
			//wait for the loading box to close
			
			int timeBuildUp = 0;
			while (true){
				for (int i = 0; i < 2; i++){
					//use to be 627 490
					robo.mouseMove(612 + i, 484);
				}
				//use to be 628 490 color 211, 132, 41
				if (robo.getPixelColor(613, 484).equals(new Color(204, 128, 42))){
					break;
				}
				timeBuildUp++;
				start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < timeBuildUp * 40);
			}
			start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 200);

			//save the page source to the given file location
			robo.mouseMove(400, 490);
			robo.mousePress(InputEvent.BUTTON3_MASK);
			robo.mouseRelease(InputEvent.BUTTON3_MASK);
			start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 500);
			robo.mouseMove(435, 575);
			robo.mousePress(InputEvent.BUTTON1_MASK);
			robo.mouseRelease(InputEvent.BUTTON1_MASK);
			//wait for the save box to come up
			while (robo.getPixelColor(436, 460).equals(new Color(255, 255, 255))){}
			start = System.currentTimeMillis();
			while(System.currentTimeMillis() - start < 500);
			//save the file in the pop up
			robo.keyPress(KeyEvent.VK_CONTROL);
			robo.keyPress(KeyEvent.VK_V);
			robo.keyRelease(KeyEvent.VK_CONTROL);
			robo.keyRelease(KeyEvent.VK_V);
			robo.keyPress(KeyEvent.VK_ENTER);
			robo.keyRelease(KeyEvent.VK_ENTER);
			//wait for override notice to come up
			//while (robo.getPixelColor(730, 380).equals(new Color(255, 255, 255))){System.out.println("in here");}
			start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 500){}
			//confirm the override
			robo.mouseMove(730, 380);
			robo.mousePress(InputEvent.BUTTON1_MASK);
			robo.mouseRelease(InputEvent.BUTTON1_MASK);
			//wait for the Chrome download pop up at bottom of screen
			while (robo.getPixelColor(600, 700).equals(new Color(255, 255, 255))){}
			start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 500);
			//wait for the download to complete
			boolean allGrey = false;
			robo.mouseMove(20, 692);
			while (!allGrey){
				allGrey = true;
				for (int i = 20; i < 23; i++){
					for (int j = 692; j < 695; j++){
						Color test = robo.getPixelColor(i, j);
						if (!((test.getRed() >= 236 && test.getRed() <= 242) && (test.getGreen() >= 236 && test.getGreen() <= 242) &&
								(test.getBlue() >= 236 && test.getBlue() <= 242))){
							allGrey = false;
						}
					}
				}
			}
			start = System.currentTimeMillis();
			while(System.currentTimeMillis() - start < 500);
			//close the download bar at bottom
			robo.mouseMove(1348, 705);
			robo.mousePress(InputEvent.BUTTON1_MASK);
			robo.mouseRelease(InputEvent.BUTTON1_MASK);
			k++;
		}
	}
	
	public static void main(String[] args){
		final UsCensusCollectionBot bot = new UsCensusCollectionBot();
		//final File f = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\wiki.htm");
		final String filename = "c:\\Users\\Jamison\\Documents\\PopulationInfo\\NewYork\\NewYork.txt";
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(100, 100);
		JButton button = new JButton("click");
		frame.add(button);
		frame.setVisible(true);

		button.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				bot.saveCityInfo(filename);
			}
		});
	}
	
}
